package com.yanqu.road.server.manager.tomb.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.dbV2.serializer.Helper;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.server.manager.tomb.entity.DbTombServerData;

import com.yanqu.road.server.manager.tomb.entity.UserAbilityInfo;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class TombServerDataDaoImpl extends TempDataObjectDao<DbTombServerData> {


    public static TombServerDataDaoImpl newInstance() {
        TombServerDataDaoImpl dao = new TombServerDataDaoImpl();
        return dao;
    }

    public static String updateSql = "update `t_c_activity_tomb_server_data` set data=?,activityUserNum=?,updateTime=? where activityId=? and groupId=? and serverId=?";

    @Override
    protected boolean updateData(DbTombServerData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getData())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityUserNum()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));

        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSql = "insert into `t_c_activity_tomb_server_data`(activityId,groupId,serverId,data,activityUserNum,createTime,updateTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DbTombServerData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getData())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityUserNum()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected DbTombServerData parseData(ResultSet rs) throws SQLException {
        DbTombServerData data = DbTombServerData.newInstance();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setServerId(rs.getLong("serverId"));
        data.setData(Helper.toObject(rs.getString("data"), new TypeReference<ArrayList<UserAbilityInfo>>() {
        }));
        if (data.getData() == null) {
            data.setData(new ArrayList<>());
        }
        data.setActivityUserNum(rs.getInt("activityUserNum"));

        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));

        return data;
    }

    public DbTombServerData getData(int activityId, int groupId, long serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_activity_tomb_server_data where activityId = ? and groupId=? and serverId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, activityId);
            call.setInt(2, groupId);
            call.setLong(3, serverId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public List<DbTombServerData> getDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DbTombServerData> list = new ArrayList<>();
        String selectSql = "select * from t_c_activity_tomb_server_data where activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DbTombServerData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public boolean saveData(DbTombServerData data) {
        if (data.isUpdateOption()) {
            if (data.isNeedInsert()) {
                if (addData(data)) {
                    data.setNeedInsert(false);
                }
            } else {
                data.setUpdateTime(System.currentTimeMillis());
                update(data);
            }
        }
        return true;
    }

}
